@extends("admin.frame_layout")
<?php $top_selected = "工作台";$page_menu = "添加新商品" ?>

@section('content')
    <div class="formbody">
        <div class="formtitle"><span>添加新的商品</span></div>
        <form action="{{route('admin.product.save')}}" method="post" id="product">
            <input name="_token" type="hidden" value="{{csrf_token()}}">
            <ul class="forminfo">
                @if(!empty($product))
                    <input type="hidden" name="id" value="{{$product->id}}"/>
                @endif
                <li>
                    <label class="lable">商品名称</label>
                    <input id="title" name="title" type="text" class="dfinput" value="{{$product->title or ""}}"/>
                    <i for="title">名称最长建议不超过100个字符</i>
                </li>
                <li>
                    <label class="lable">是否上架</label>
                    <cite>
                        <?php
                        $status = \App\Models\MProduct::$STATUS_DOWN;
                        if (!empty($product)) {
                            $status = $product->status;
                        }
                        ?>
                        <input name="status" type="radio"
                               value="0" {{$status == \App\Models\MProduct::$STATUS_SALE?"checked":""}}/>直接上架&nbsp;&nbsp;&nbsp;&nbsp;
                        <input name="status" type="radio"
                               value="1" {{$status == \App\Models\MProduct::$STATUS_DOWN?"checked":""}}/>暂不上架
                    </cite>
                </li>
                <li>
                    <label class="lable">商品图片</label>
                    <input id="file" name="file" type="file" class="dfinput"/>
                    <i for="file">图片最佳尺寸 宽：600px 高：600px</i>
                </li>
                @if(isset($product) && !empty($product->img))
                    <li>
                        <label class="lable">&nbsp;</label>
                        <img id="img0" src="{{asset($product->img)}}" width="200" height="200"
                             onclick="$('#file').click()"/>
                    </li>
                @endif
                {!! inputRow("商品价格",
                    [
                        ["id"=>"price1", "type"=>"number", "value"=>isset($product)?$product->p1:"", "des"=>"商品 现价，促销价 ！"],
                        ["id"=>"price2", "type"=>"number", "value"=>isset($product)?$product->p2:"", "des"=>"商品 原价 ！"],
                    ],
                "请分别填写商品用不同货币购买的价格！") !!}
                <li>
                    <label class="lable">单次购买数量</label>
                    <input id="unit" name="unit" type="number" class="dfinput"
                           value="{{$product->unit or "1"}}"/>
                    <i for="unit">商品最低的购买数量</i>
                </li>
                <li>
                    <label class="lable">商品库存</label>
                    <input id="num" name="num" type="number" class="dfinput" value="{{$product->num or "999999999"}}"/>
                    <i for="num"></i>
                </li>
                <li>
                    <label class="lable">商品详情描述</label>
                    {{-- 加载编辑器的容器 --}}
                    <div style="float: left;width: 800px;">
                        <script name="detail" id="detail"
                                type="text/plain">{!! $product->detail or "" !!}</script>
                    </div>
                    <div style="clear: both;"></div>
                    {{--<label class="lable">商品详情描述</label>--}}
                    {{--<textarea id="detail" name="detail" cols="" rows=""--}}
                    {{--class="textinput">{{$product->detail or ""}}</textarea>--}}
                    <i for="detail"></i>
                </li>
                <li><label class="lable">&nbsp;</label>
                    <input type="submit" class="btn" value="添加商品"/>
                </li>
            </ul>
        </form>
    </div>
@endsection

@section('footer')
    <script src="{{asset('admin/js/ueditor/ueditor.config.js')}}"></script>
    <script src="{{asset('admin/js/ueditor/ueditor.all.js')}}"></script>
    <script type="text/javascript">
        //初始化编辑器
        var ue = UE.getEditor('detail', {
            initialFrameHeight: 250
        });

        initCalcText();
        $(".calc").hover(function () {
            $(this).closest("div").find('i').show();
        }, function () {
            $(this).closest("div").find('i').hide();
        });
        $("#product").validate({
            rules: {
                title: "required",
                status: "required",
                //pic: "required",
                price1: "required",
                price2: "required",
                unit: "required",
                num: "required",
                detail: "required",
            }, errorPlacement: function (error, element) {
                $(element).closest("form").find("i[for='" + element.attr("id") + "']").html(error);
            },
            submitHandler: function (form) {
                $(form).ajaxSubmit({
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        if (data.code == 0) {
                            toastr["success"](data.msg);
                            window.location = "{{route('admin.product.list',['flag'=>0])}}";
                        } else {
                            toastr["error"](data.msg);
                        }
                    }
                });
            }
        });

        $("#file").change(function () {
            var objUrl = getObjectURL(this.files[0]);
            console.log("objUrl = " + objUrl);
            if (objUrl) {
                $("#img0").attr("src", objUrl);
            }
        });
        function getObjectURL(file) {
            var url = null;
            if (window.createObjectURL != undefined) { // basic
                url = window.createObjectURL(file);
            } else if (window.URL != undefined) { // mozilla(firefox)
                url = window.URL.createObjectURL(file);
            } else if (window.webkitURL != undefined) { // webkit or chrome
                url = window.webkitURL.createObjectURL(file);
            }
            return url;
        }
    </script>
@endsection
